import { defineStore } from 'pinia'

export default defineStore('user', {
    state: () => ({
        token: uni.getStorageSync('pinia_token') || null,
		userInfo: uni.getStorageSync('pinia_user_info') || null,
    }),
	
	getters: {
		islogin: (state) => !!state.token,
	},
	
	actions: {
		signoutFun() {
			this.token = null
			this.userdata = null
			this.userInfo = null
			uni.removeStorageSync('pinia_token')
			uni.removeStorageSync('pinia_user_info')
			uni.removeStorageSync('subscribeId')
		},
		
		setToken(token) {  // 存储登录标识
			this.token = token
			uni.setStorageSync('pinia_token', token)
		},
		
		setuserInfo(data) {  // 设置用户个人信息
			this.userInfo = data
			uni.setStorageSync('pinia_user_info', data)
		},
		
		oneself(id) {  // 否是自己
			const { id: userId } = this.userInfo
			return userId == id
		},

		async loginOut() {  //退出登录
			
			this.signoutFun()
		}
	}
})